﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL.MedicalGraph;
using System.Collections.Generic;

namespace web.admin
{
    public partial class GraphSymptom : System.Web.UI.Page
    {
        GraphSetting SymptomSetting;
        DiseaseGraphSettingInfo SettingInfo;
        List<DiseaseSymptomInfo> SymptomList;
        int GroupIndex = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                HandleGet();
            }
            else
            {
                HandlePost();
            }
        }

        protected void HandleGet()
        {
            int dszid = RequestTools.GetQueryInt("id",-1);
            HiddenDiseaseId.Value = dszid.ToString();
            if (dszid < 0)
            {
                this.AddErrorMessage("无效的参数");
                return;
            }
            GetSetting(dszid);
            BindAll();
        }

        protected void HandlePost()
        {
            int dszid = Utils.StrToInt(HiddenDiseaseId.Value, -1);
            if (dszid < 0) return;
            GetSetting(dszid);
        }

       
        public void ClearErrorMessage()
        {
            LabelErrorMsg.Text = "";
        }
        public void AddErrorMessage(string msg)
        {
            LabelErrorMsg.Text += "\r\n" + msg;
        }

        protected void GetSetting()
        {
            int dszid = Utils.StrToInt(HiddenDiseaseId.Value, -1);
            if (dszid < 0) return;
            GetSetting(dszid);
        }

        public void GetSetting(int dszid)
        {
            SettingInfo = DiseaseBLL.Instance.GetDiseaseGraphSettingInfo(dszid);
            try
            {
                SymptomSetting = SerializationHelper.DeserializeFromXml<GraphSetting>(SettingInfo.GraphSymptomSetting);
            }
            catch
            {
                SymptomSetting = new GraphSetting();
            }
            if (SettingInfo.DiseaseId <= 0) SettingInfo.DiseaseId = dszid;
            if (SymptomSetting.ParamList.Count == 0) SymptomSetting.ParamList.Add(new GraphRule());
            SymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(dszid);
        }

        public void SaveSetting()
        {
            SettingInfo.GraphSymptomSetting = SerializationHelper.SerializeToXml(SymptomSetting);
            if (SettingInfo.Id <= 0) DiseaseBLL.Instance.AddDiseaseGraphSetting(SettingInfo);
            else DiseaseBLL.Instance.UpdateDiseaseGraphSetting(SettingInfo);
        }

        #region 组 

        protected void RefreshData()
        {
            SaveSetting();
            GetSetting();
            BindAll();
        }

        protected void BindAll()
        {
            BindGroup();
            BindSymptom();
        }

        protected void BindSymptom()
        {
            LsbSymList.Items.Clear();
            foreach (DiseaseSymptomInfo info in SymptomList)
            {
                LsbSymList.Items.Add(new ListItem(info.SymptomName, info.SymptomId.ToString()));
            }
        }

        protected void BindGroup()
        {
            GraphRule rule = SymptomSetting.ParamList[0];
            this.LsbGroupList.Items.Clear();
            int i=0;
            foreach (GraphRule.Group group in rule.mGroups)
            {
                ListItem item = new ListItem(group.mName, i.ToString());
                if (i == GroupIndex) item.Selected = true;
                this.LsbGroupList.Items.Add(item);
                i++;
            }
            
            BindVariable(GroupIndex);
        }


        //删除组
        protected void BtnDelGroup_Click(object sender, EventArgs e)
        {
            int idx = LsbGroupList.SelectedIndex;
            if (idx < 0) return;
            GraphRule rule = SymptomSetting.ParamList[0];
            rule.mGroups.RemoveAt(idx);
            RefreshData();
            
        }

        protected void BtnRenameGroup_Click(object sender, EventArgs e)
        {
            int idx = LsbGroupList.SelectedIndex;
            if (idx < 0) return;
            GraphRule rule = SymptomSetting.ParamList[0];
            rule.mGroups[idx].mName = this.TxbGroupRename.Text;
            this.TxbGroupRename.Text = "";
            RefreshData();
        }

        protected void BtnAddGroup_Click(object sender, EventArgs e)
        {
            GraphRule rule = SymptomSetting.ParamList[0];
            GraphRule.Group group = new GraphRule.Group();
            group.mName = "新建组";
            rule.mGroups.Add(group);
            RefreshData();
        }

        protected void LsbGroupList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindVariable(LsbGroupList.SelectedIndex);
        }

        #endregion


        void BindVariable(int idx)
        {
            GraphRule rule = SymptomSetting.ParamList[0];
            if (rule.mGroups == null || idx >= rule.mGroups.Count) return; 
            GraphRule.Group group = rule.mGroups[idx];
            LsbVarList.Items.Clear();
            int i = 0;
            foreach (GraphRule.Variable var in group.mVariables)
            {
                LsbVarList.Items.Add(new ListItem(var.mName,i.ToString()));
                i ++;
            }
        }

        protected void BtnAddVar_Click(object sender, EventArgs e)
        {
            int symidx = LsbSymList.SelectedIndex;
            if (symidx < 0) return;
            int grpidx = LsbGroupList.SelectedIndex;
            if (grpidx < 0) return;
            GroupIndex = grpidx;
            GraphRule rule = SymptomSetting.ParamList[0];
            GraphRule.Group group = rule.mGroups[grpidx];
            DiseaseSymptomInfo info = SymptomList[symidx];
            GraphRule.Variable var = new GraphRule.Variable();
            var.mName = info.SymptomName;
            var.mKey = info.SymptomId.ToString();
            group.mVariables.Add(var);
            RefreshData();

        }

        protected void BtnDelVar_Click(object sender, EventArgs e)
        {
            int idx = LsbVarList.SelectedIndex;
            if (idx < 0) return;
            int grpidx = LsbGroupList.SelectedIndex;
            if (grpidx < 0) return;
            GroupIndex = grpidx;
            GraphRule rule = SymptomSetting.ParamList[0];
            GraphRule.Group group = rule.mGroups[grpidx];
            group.mVariables.RemoveAt(idx);
            RefreshData();
        }

        protected void BtnUpVar_Click(object sender, EventArgs e)
        {
            int idx = LsbVarList.SelectedIndex;
            if (idx <= 0) return;
            int grpidx = LsbGroupList.SelectedIndex;
            if (grpidx < 0) return;
            GroupIndex = grpidx;
            GraphRule rule = SymptomSetting.ParamList[0];
            GraphRule.Group group = rule.mGroups[grpidx];
            GraphRule.Variable var = group.mVariables[idx];
            group.mVariables.RemoveAt(idx);
            group.mVariables.Insert(idx - 1, var);
            RefreshData();
        }

        protected void BtnDownVar_Click(object sender, EventArgs e)
        {
            int idx = LsbVarList.SelectedIndex;
            if (idx >= LsbVarList.Items.Count - 1 ) return;
            int grpidx = LsbGroupList.SelectedIndex;
            if (grpidx < 0) return;
            GroupIndex = grpidx;
            GraphRule rule = SymptomSetting.ParamList[0];
            GraphRule.Group group = rule.mGroups[grpidx];
            GraphRule.Variable var = group.mVariables[idx + 1];
            group.mVariables.RemoveAt(idx+1);
            group.mVariables.Insert(idx, var);
            RefreshData();
        }

       
    }
}
